package study.alex.resumedb.gae;

import com.vaadin.addon.jpacontainer.EntityProvider;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.addon.jpacontainer.provider.CachingMutableLocalEntityProvider;
import com.vaadin.addon.jpacontainer.util.DefaultQueryModifierDelegate;
import study.alex.resumedb.gae.model.Resume;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import java.io.Serializable;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: alex
 * Date: 23.01.14
 * Time: 16:21
 * SVN $Id$
 */
public class JPAContainerForResume extends JPAContainer<Resume> implements Serializable {

    private void readObject(java.io.ObjectInputStream stream)
            throws java.io.IOException, ClassNotFoundException
    {
        stream.defaultReadObject();

        // assign reference manually.
        EntityProvider<Resume> entityProvider =
                new CachingMutableLocalEntityProvider<Resume>(Resume.class, JPAContainerFactory.createEntityManagerForPersistenceUnit(EMF.PERSISTENCE_UNIT));
        setEntityProvider(entityProvider);

        getEntityProvider().setQueryModifierDelegate(
                new DefaultQueryModifierDelegate() {
                    @Override
                    public void queryHasBeenBuilt(CriteriaBuilder criteriaBuilder, CriteriaQuery<?> query) {
                        Logger.getLogger(JPAContainerForResume.class.getName()).fine("Filter query: " + query);
                        super.queryHasBeenBuilt(criteriaBuilder, query);
                    }
                });

    }

    public JPAContainerForResume() {
        super(Resume.class);
    }
}
